
package com.googlecode.esblabs.util.mule.transformer.jaxb;

import java.io.StringReader;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;


public class JaxbXmlToObject extends AbstractJaxbTransformer
{

    public JaxbXmlToObject()
    {
        registerSourceType(String.class);
        setReturnClass(Object.class);
    }

    public Object transform(MuleMessage message, String outputEncoding) throws TransformerException
    {
        String src = (String)message.getPayload();
        try
        {
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Object o = unmarshaller.unmarshal(new StringReader(src));
            if (logger.isDebugEnabled()) logger.debug("transform(String to " + o.getClass().getSimpleName() + ", " + outputEncoding + ") returns: " +  o.toString());
            return o;
        }
        catch (JAXBException e)
        {
            throw new TransformerException(this, e);
        }
    }

}
